//========================================================================
//  SE5 AI Global Research Settings - Captain Kwok's Balance Mod v125+
//========================================================================
//
// Note: This script is only used during in the creation of a game
//
// Balance Mod Changes:
// --------------------
// v1.25 Changed - Updated for tech area changes in v1.25
//       Changed   Added flag for racial trait technologies
// v1.23 Fixed   - Some tech area requirement levels were incorrectly referenced
//       Changed - Removed some tech level checks that were not necessary
// v1.20 Changed - Updated tech area name/levels
// v1.19 Changed - Replaced old tech selection routine with one to complement new individual AI selection routine
//       Fixed   - A number of errors in maximum tech levels
// v1.15 Changed - Updated tech area names and levels
//       Fixed   - AI was not selecting starting technologies properly
// v1.10 Fixed   - Corrections for tech level changes
//       Changed - Streamlined selection
// v1.05 Fixed   - Corrections for max levels and changes to tech tree
// v1.00 Fixed   - AI during setup wasn't selecting starting technologies
//       Changed - Added conditional statements for Bombardment Weapons and Warheads
//       Changed - Updated tech prerequisites
// v0.99 Changed - Updated tech list to match recent changes in Script_AI_Research.txt
// v0.93 Changed - Updated tech list to match Script_AI_Research.txt
//
// Balance Mod To-do List:
// -----------------------
// - Nothing pending
//
// Script Function Requests:
// -------------------------
// - Nothing pending

//------------------------------------------------------------------------
// Global Variables
//------------------------------------------------------------------------
globalvars

  lng_Setup_Research_Age_Max_Level:                        long := 0

  bool_Intel_Is_Allowed:                                   boolean := TRUE
  bool_Race_Uses_Mines:                                    boolean := TRUE
  bool_Race_Uses_Fighters:                                 boolean := TRUE
  bool_Race_Uses_Satellites:                               boolean := TRUE
  bool_Race_Uses_Troops:                                   boolean := TRUE
  bool_Race_Uses_Weapon_Platforms:                         boolean := TRUE
  bool_Race_Uses_Drones:                                   boolean := FALSE
  bool_Race_Uses_Kamikaze_Ships:                           boolean := FALSE
  bool_Race_Uses_Boarding_Ships:                           boolean := FALSE
  bool_Race_Uses_Bombardment_Ships:                        boolean := FALSE
  bool_Race_Uses_Plague_Ships:                             boolean := FALSE
  bool_Race_Uses_Stellar_Manipulation_Ships:               boolean := FALSE
  bool_Race_Uses_Evil_Stellar_Manipulation_Ships:          boolean := FALSE
  bool_Race_Uses_Normal_Armor:                             boolean := TRUE
  bool_Race_Uses_Normal_Shields:                           boolean := TRUE
  bool_Race_Uses_Cloaking_Devices:                         boolean := FALSE
  bool_Race_Uses_Master_Computers:                         boolean := FALSE

  bool_Race_Uses_Racial_Tech:                              boolean := FALSE
  bool_Race_Uses_Crystalline_Tech:                         boolean := FALSE
  bool_Race_Uses_Interdimensional_Tech:                    boolean := FALSE
  bool_Race_Uses_Organic_Tech:                             boolean := FALSE
  bool_Race_Uses_Psychic_Tech:                             boolean := FALSE
  bool_Race_Uses_Religious_Tech:                           boolean := FALSE
  bool_Race_Uses_Temporal_Tech:                            boolean := FALSE

  bool_Race_Uses_Crystalline_Weapons:                      boolean := FALSE
  bool_Race_Uses_Interdimensional_Weapons:                 boolean := FALSE
  bool_Race_Uses_Organic_Weapons:                          boolean := FALSE
  bool_Race_Uses_Psychic_Weapons:                          boolean := FALSE
  bool_Race_Uses_Temporal_Weapons:                         boolean := FALSE

  bool_Race_Uses_Projectile_Weapons:                       boolean := FALSE
  bool_Race_Uses_Focused_Energy_Weapons:                   boolean := FALSE
  bool_Race_Uses_Missile_Weapons:                          boolean := FALSE
  bool_Race_Uses_Energy_Stream_Weapons:                    boolean := FALSE
  bool_Race_Uses_Energy_Pulse_Weapons:                     boolean := FALSE
  bool_Race_Uses_Plasma_Missile_Weapons:                   boolean := FALSE
  bool_Race_Uses_Torpedo_Weapons:                          boolean := FALSE
  bool_Race_Uses_Phased_Energy_Weapons:                    boolean := FALSE
  bool_Race_Uses_High_Energy_Discharge_Weapons:            boolean := FALSE

  bool_Race_Uses_Shield_Damaging_Weapons:                  boolean := FALSE
  bool_Race_Uses_Engine_Overloading_Weapons:               boolean := FALSE
  bool_Race_Uses_Weapon_Overloading_Weapons:               boolean := FALSE

  bool_Race_Uses_Gravitational_Weapons:                    boolean := FALSE
  bool_Race_Uses_Warp_Weapons:                             boolean := FALSE
  bool_Race_Uses_Tractor_Repulser_Weapons:                 boolean := FALSE
  bool_Race_Uses_Biological_Weapons:                       boolean := FALSE
  bool_Race_Uses_Bombardment_Weapons:                      boolean := FALSE

endglobalvars

//------------------------------------------------------------------------
// Get_Required_Techs_For_Tech
//------------------------------------------------------------------------
function Get_Required_Techs_For_Tech returns boolean
params
  src_tech_name:             string
  tech_name:                 string byref
  tech_level:                long byref
begin
  set tech_name := ""
  set tech_level := 0

  case (src_tech_name)
    "Civics":
      set tech_name := "Cultural Studies"
      set tech_level := 1
    "Conservation":
      set tech_name := "Cultural Studies"
      set tech_level := 1
    "Diplomacy":
      set tech_name := "Applied Political Science"
      set tech_level := 1
    "Economics":
      set tech_name := "Cultural Studies"
      set tech_level := 1
    "Education":
      set tech_name := "Cultural Studies"
      set tech_level := 1
    "Engineering":
      set tech_name := "Construction"
      set tech_level := 2
    "Environment":
      set tech_name := "Biology"
      set tech_level := 2
    "Ethics":
      set tech_name := "Cultural Studies"
      set tech_level := 1
    "Logistics":
      set tech_name := "Industry"
      set tech_level := 2
    "Military":
      set tech_name := "Advanced Military Science"
      set tech_level := 1
    "Recreation":
      set tech_name := "Cultural Studies"
      set tech_level := 1
    "Sociology":
      set tech_name := "Psychology"
      set tech_level := 1
    "Medium Ship Construction":
      set tech_name := "Small Ship Construction"
      set tech_level := 4
    "Large Ship Construction":
      set tech_name := "Medium Ship Construction"
      set tech_level := 6
    "Baseship Construction":
      set tech_name := "Large Ship Construction"
      set tech_level := 6
    "Large Base Construction":
      set tech_name := "Small Base Construction"
      set tech_level := 4
    "Mines":
      set tech_name := "Construction"
      set tech_level := 2
    "Drones":
      set tech_name := "Computers"
      set tech_level := 1
    "Gravitational Manipulation":
      set tech_name := "Astrophysics"
      set tech_level := 3
    "Computers":
      set tech_name := "Industry"
      set tech_level := 2
    "Contra-Terrene Engines":
      set tech_name := "Ion Engines"
      set tech_level := 3
    "Jacketed-Photon Engines":
      set tech_name := "Contra-Terrene Engines"
      set tech_level := 3
    "Quantum Engines":
      set tech_name := "Jacketed-Photon Engines"
      set tech_level := 3
    "Stellar Harnessing":
      set tech_name := "Astrophysics"
      set tech_level := 1
    "Armor":
      set tech_name := "Chemistry"
      set tech_level := 1
    "Shields":
      set tech_name := "Physics"
      set tech_level := 2
    "Null-Space Shields":
      set tech_name := "Astrophysics"
      set tech_level := 3
    "Cloaking":
      set tech_name := "Physics"
      set tech_level := 4
    "Scanners":
      set tech_name := "Physics"
      set tech_level := 3
    "Defense Systems":
      set tech_name := "Military Science"
      set tech_level := 2
    "Ship Weapon Mounts":
      set tech_name := "Weapon Mounts"
      set tech_level := 1
    "Base Weapon Mounts":
      set tech_name := "Weapon Mounts"
      set tech_level := 1
    "Unit Weapon Mounts":
      set tech_name := "Weapon Mounts"
      set tech_level := 1
    "Stellar Manipulation":
      set tech_name := "Astrophysics"
      set tech_level := 5
    "Storm Manipulation":
      set tech_name := "Stellar Manipulation"
      set tech_level := 1
    "Planetary Manipulation":
      set tech_name := "Stellar Manipulation"
      set tech_level := 2
    "Warp Point Manipulation":
      set tech_name := "Stellar Manipulation"
      set tech_level := 3
    "Star Manipulation":
      set tech_name := "Stellar Manipulation"
      set tech_level := 4
    "Nebulae Manipulation":
      set tech_name := "Stellar Manipulation"
      set tech_level := 4
    "Black Hole Manipulation":
      set tech_name := "Stellar Manipulation"
      set tech_level := 5
    "Stellar Construction":
      set tech_name := "Stellar Manipulation"
      set tech_level := 6
    "Advanced Military Science":
      set tech_name := "Military Science"
      set tech_level := 3
    "Applied Political Science":
      set tech_name := "Psychology"
      set tech_level := 1
    "Medical Treatment":
      set tech_name := "Biology"
      set tech_level := 1
    "Robotics":
      set tech_name := "Construction"
      set tech_level := 3
    "Centralized Computer Systems":
      set tech_name := "Industry"
      set tech_level := 3
    "Planetary Defenses":
      set tech_name := "Shields"
      set tech_level := 5
    "Planet Utilization":
      set tech_name := "Planetary Engineering"
      set tech_level := 1
    "Resource Manipulation":
      set tech_name := "Chemistry"
      set tech_level := 3
    "Sabotage - Ships & Fleets":
      set tech_name := "Intelligence Services"
      set tech_level := 1
    "Sabotage - Planets & Colonies":
      set tech_name := "Intelligence Services"
      set tech_level := 2
    "Sabotage - Empire Wide":
      set tech_name := "Intelligence Services"
      set tech_level := 3
    "Sabotage - Political":
      set tech_name := "Intelligence Services"
      set tech_level := 4
    "Espionage - Ships & Fleets":
      set tech_name := "Intelligence Services"
      set tech_level := 1
    "Espionage - Planets & Colonies":
      set tech_name := "Intelligence Services"
      set tech_level := 2
    "Espionage - Empire Wide":
      set tech_name := "Intelligence Services"
      set tech_level := 3
    "Espionage - Political":
      set tech_name := "Intelligence Services"
      set tech_level := 4
    "Cooperative Intelligence":
      set tech_name := "Intelligence Services"
      set tech_level := 5
    "Energy Stream Weapons":
      set tech_name := "Physics"
      set tech_level := 2
    "Energy Pulse Weapons":
      set tech_name := "Physics"
      set tech_level := 2
    "Plasma Missile Weapons":
      set tech_name := "Chemistry"
      set tech_level := 2
    "Torpedo Weapons":
      set tech_name := "Military Science"
      set tech_level := 3
    "High-Energy Discharge Weapons":
      set tech_name := "Physics"
      set tech_level := 3
    "Phased-Energy Weapons":
      set tech_name := "Physics"
      set tech_level := 3
    "Shield Damaging Weapons":
      set tech_name := "Shields"
      set tech_level := 3
    "Tractor\Repulser Weapons":
      set tech_name := "Astrophysics"
      set tech_level := 2
    "Weapon Overloading Weapons":
      set tech_name := "Astrophysics"
      set tech_level := 2
    "Engine Overloading Weapons":
      set tech_name := "Ion Engines"
      set tech_level := 3
    "Gravitational Weapons":
      set tech_name := "Astrophysics"
      set tech_level := 3
    "Warp Weapons":
      set tech_name := "Physics"
      set tech_level := 5
    "Computer Combat":
      set tech_name := "Computers"
      set tech_level := 3
    "Null-Space Weapons":
      set tech_name := "Astrophysics"
      set tech_level := 3
    "Ship Capture":
      set tech_name := "Military Science"
      set tech_level := 2
    "Biological Weapons":
      set tech_name := "Biology"
      set tech_level := 2
  endcase

  return (tech_level > 0)
end

//------------------------------------------------------------------------
// Add_Racial_Trait
//------------------------------------------------------------------------
function Add_Racial_Trait returns boolean
params
  trait_name:                string
vars
  num_points_to_spend:       long
  this_cost:                 long
  trait_added:               boolean := FALSE
begin

  // How many points do we have to spend?
  set num_points_to_spend := Sys_AI_Setup_Get_Racial_Points_Remaining(sys_long_Player_ID)
  set this_cost := Sys_AI_Setup_Get_Racial_Trait_Cost(trait_name)

  // Attempt to add the desired racial trait
  if (this_cost <= num_points_to_spend) and (num_points_to_spend > 0) then
    set trait_added := Sys_AI_Setup_Select_Racial_Trait(sys_long_Player_ID, trait_name)
    // Debug output
    call Sys_Debug_Print("Setup", "    - Added racial trait " + trait_name + " for " + Sys_Convert_Long_To_String(this_cost) + " racial points")
    call Sys_Debug_Print("Setup", "      - " + Sys_Convert_Long_To_String(num_points_to_spend - this_cost) + " racial points remaining")
  endif

  return trait_added
end

//------------------------------------------------------------------------
// Add_Tech_Area
//------------------------------------------------------------------------
function Add_Tech_Area returns boolean
params
  tech_area:                 string
  wanted_tech_level:         long
  add_tech_area:             boolean
  add_one_level:             boolean
vars
  num_points_to_spend:       long
  this_cost:                 long
  tech_id:                   long
  curr_tech_level:           long
  next_tech_level:           long
  tech_level_index:          long
  base_tech_name:            string
  base_tech_level:           long
  add_base_tech:             boolean
begin

  // Pick our starting technology
  if (add_tech_area) then
    set tech_id := Sys_Get_Tech_ID(tech_area)
    // Is the tech area available?
    if Sys_Is_Empire_Research_Tech_Visible(sys_long_Player_ID, tech_id) then
      set curr_tech_level := Sys_AI_Setup_Get_Tech_Level(sys_long_Player_ID, tech_id)
      // Debug output
      call Sys_Debug_Print("Setup", "    - Want to add " + tech_area + " Level " + Sys_Convert_Long_To_String(wanted_tech_level) + "; Current level = " + Sys_Convert_Long_To_String(curr_tech_level))
      // Is our current tech level less than the wanted tech level?
      if (curr_tech_level < wanted_tech_level) then
        set next_tech_level := curr_tech_level + 1
        // For random tech selection or single tech selection
        if (add_one_level) then
          set wanted_tech_level := curr_tech_level + 1
        endif
        // Attempt to select the tech area
        for tech_level_index := next_tech_level to wanted_tech_level do
          if (not Sys_Is_Empire_Research_Tech_At_Maximum(sys_long_Player_ID, tech_id)) then
            set num_points_to_spend := Sys_AI_Setup_Get_Research_Points_Remaining(sys_long_Player_ID)
            set this_cost := Sys_AI_Setup_Get_Tech_Area_Cost(sys_long_Player_ID, tech_id, tech_level_index)
            // Do we have enough points to pick the tech area?
            if (this_cost <= num_points_to_spend) then
              call Sys_AI_Setup_Select_Tech_Area(sys_long_Player_ID, tech_id, tech_level_index)
              // Debug output
              call Sys_Debug_Print("Setup", "      - Added tech area " + tech_area + " level " + Sys_Convert_Long_To_String(tech_level_index) + " for " + Sys_Convert_Long_To_String(this_cost) + " research points")
              call Sys_Debug_Print("Setup", "        - " + Sys_Convert_Long_To_String(num_points_to_spend - this_cost) + " research points remaining")
            endif
          endif
        endfor
      endif
    else
      // Get the information for the tech area's prerequisite
      set add_base_tech := Get_Required_Techs_For_Tech(tech_area, base_tech_name, base_tech_level)
      // Attempt to add the prerequisite
      if (add_base_tech) then
        if (base_tech_level > 0) then
          set tech_id := Sys_Get_Tech_ID(base_tech_name)
          // Is the prerequisite visible?
          if Sys_Is_Empire_Research_Tech_Visible(sys_long_Player_ID, tech_id) then
            set curr_tech_level := Sys_AI_Setup_Get_Tech_Level(sys_long_Player_ID, tech_id)
            // Is our current tech level less than the wanted tech level
            if (curr_tech_level < wanted_tech_level) then
              set curr_tech_level := curr_tech_level + 1
              // Attempt to select the tech area
              for tech_level_index := curr_tech_level to wanted_tech_level do
                if (not Sys_Is_Empire_Research_Tech_At_Maximum(sys_long_Player_ID, tech_id)) then
                  set num_points_to_spend := Sys_AI_Setup_Get_Research_Points_Remaining(sys_long_Player_ID)
                  set this_cost := Sys_AI_Setup_Get_Tech_Area_Cost(sys_long_Player_ID, tech_id, tech_level_index)
                  // Do we have enough points to pick the tech area?
                  if (this_cost <= num_points_to_spend) then
                    call Sys_AI_Setup_Select_Tech_Area(sys_long_Player_ID, tech_id, tech_level_index)
                    // Debug output
                    call Sys_Debug_Print("Setup", "      - Added base tech area " + base_tech_name + " level " + Sys_Convert_Long_To_String(tech_level_index) + " for " + Sys_Convert_Long_To_String(this_cost) + " research points")
                    call Sys_Debug_Print("Setup", "        - " + Sys_Convert_Long_To_String(num_points_to_spend - this_cost) + " research points remaining")
                  endif
                endif
              endfor
            endif
          endif
        endif
      endif
    endif
  endif

  return TRUE
end

//------------------------------------------------------------------------
// AI_Research_Selection
//------------------------------------------------------------------------
function AI_Research_Selection returns boolean
params
vars
  loop_count:                long := 0
  research_pts_left:         long
begin

  // Note game settings
  set bool_Intel_Is_Allowed := Sys_Is_Game_Using_Intelligence()

  // Are we using any racial traits?
  set bool_Race_Uses_Crystalline_Tech := Sys_Is_Empire_Research_Tech_Visible(sys_long_Player_ID, Sys_Get_Tech_ID("Crystalline Technology"))
  set bool_Race_Uses_Interdimensional_Tech := Sys_Is_Empire_Research_Tech_Visible(sys_long_Player_ID, Sys_Get_Tech_ID("Interdimensional Technology"))
  set bool_Race_Uses_Organic_Tech := Sys_Is_Empire_Research_Tech_Visible(sys_long_Player_ID, Sys_Get_Tech_ID("Organic Technology"))
  set bool_Race_Uses_Psychic_Tech := Sys_Is_Empire_Research_Tech_Visible(sys_long_Player_ID, Sys_Get_Tech_ID("Psychic Technology"))
  set bool_Race_Uses_Religious_Tech := Sys_Is_Empire_Research_Tech_Visible(sys_long_Player_ID, Sys_Get_Tech_ID("Religious Technology"))
  set bool_Race_Uses_Temporal_Tech := Sys_Is_Empire_Research_Tech_Visible(sys_long_Player_ID, Sys_Get_Tech_ID("Temporal Technology"))

  set bool_Race_Uses_Crystalline_Weapons := bool_Race_Uses_Crystalline_Tech
  set bool_Race_Uses_Interdimensional_Weapons := bool_Race_Uses_Interdimensional_Tech
  set bool_Race_Uses_Organic_Weapons := bool_Race_Uses_Organic_Tech
  set bool_Race_Uses_Psychic_Weapons := bool_Race_Uses_Psychic_Tech
  set bool_Race_Uses_Temporal_Weapons := bool_Race_Uses_Temporal_Tech

  // Add Tech Areas in a partial-random manner
  loop
    set loop_count := loop_count + 1
    set research_pts_left := Sys_AI_Setup_Get_Research_Points_Remaining(sys_long_Player_ID)
    // Select techs at random
    case Sys_Get_Random_Long(1, 6)
      1:
        // Cultural
        case Sys_Get_Random_Long(1, 13)
          1:
            call Add_Tech_Area("Cultural Studies", 1, TRUE, TRUE)
          2:
            call Add_Tech_Area("Civics", 10, TRUE, TRUE)
          3:
            call Add_Tech_Area("Conservation", 10, TRUE, TRUE)
          4:
            call Add_Tech_Area("Diplomacy", 10, TRUE, TRUE)
          5:
            call Add_Tech_Area("Economics", 10, TRUE, TRUE)
          6:
            call Add_Tech_Area("Education", 10, TRUE, TRUE)
          7:
            call Add_Tech_Area("Engineering", 10, TRUE, TRUE)
          8:
            call Add_Tech_Area("Environment", 10, TRUE, TRUE)
          9:
            call Add_Tech_Area("Ethics", 10, TRUE, TRUE)
          10:
            call Add_Tech_Area("Logistics", 10, TRUE, TRUE)
          11:
            call Add_Tech_Area("Military", 10, TRUE, TRUE)
          12:
            call Add_Tech_Area("Recreation", 10, TRUE, TRUE)
          13:
            call Add_Tech_Area("Sociology", 10, TRUE, TRUE)
        endcase
      2:
        // Theoretical Technologies
        case Sys_Get_Random_Long(1, 9)
          1:
            call Add_Tech_Area("Astrophysics", 5, TRUE, TRUE)
          2:
            call Add_Tech_Area("Biology", 2, TRUE, TRUE)
          3:
            call Add_Tech_Area("Chemistry", 3, TRUE, TRUE)
          4:
            call Add_Tech_Area("Construction", 3, TRUE, TRUE)
          5:
            call Add_Tech_Area("Industry", 3, TRUE, TRUE)
          6:
            call Add_Tech_Area("Military Science", 3, TRUE, TRUE)
          7:
            call Add_Tech_Area("Physics", 4, TRUE, TRUE)
          8:
            call Add_Tech_Area("Planetary Engineering", 1, TRUE, TRUE)
          9:
            call Add_Tech_Area("Psychology", 1, TRUE, TRUE)
        endcase
      3:
        // Colonization
        case Sys_Get_Random_Long(1, 3)
          1:
            call Add_Tech_Area("Rock Planet Colonization", 1, TRUE, TRUE)
          2:
            call Add_Tech_Area("Ice Planet Colonization", 1, TRUE, TRUE)
          3:
            call Add_Tech_Area("Gas Giant Colonization", 1, TRUE, TRUE)
        endcase
      4:
        // Racial Technologies
        case Sys_Get_Random_Long(1, 6)
          1:
            call Add_Tech_Area("Crystalline Technology", 20, bool_Race_Uses_Crystalline_Tech, TRUE)
          2:
            call Add_Tech_Area("Interdimensional Technology", 15, bool_Race_Uses_Interdimensional_Tech, TRUE)
          3:
            call Add_Tech_Area("Organic Technology", 20, bool_Race_Uses_Organic_Tech, TRUE)
          4:
            call Add_Tech_Area("Psychic Technology", 15, bool_Race_Uses_Psychic_Tech, TRUE)
          5:
            call Add_Tech_Area("Religious Technology", 20, bool_Race_Uses_Religious_Tech, TRUE)
          6:
            call Add_Tech_Area("Temporal Technology", 15, bool_Race_Uses_Temporal_Tech, TRUE)
        endcase
      5:
        // Applied Science
        case Sys_Get_Random_Long(1, 11)
          1:
            // Ship Hulls
            case Sys_Get_Random_Long(1, 4)
              1:
                call Add_Tech_Area("Small Ship Construction", 6, TRUE, TRUE)
              2:
                call Add_Tech_Area("Medium Ship Construction", 9, TRUE, TRUE)
              3:
                call Add_Tech_Area("Large Ship Construction", 9, TRUE, TRUE)
              4:
                call Add_Tech_Area("Baseship Construction", 6, TRUE, TRUE)
            endcase
          2:
            // Base Hulls
            case Sys_Get_Random_Long(1, 2)
              1:
                call Add_Tech_Area("Small Base Construction", 6, TRUE, TRUE)
              2:
                call Add_Tech_Area("Large Base Construction", 6, TRUE, TRUE)
            endcase
          3:
            // Units
            case Sys_Get_Random_Long(1, 6)
              1:
                call Add_Tech_Area("Troops", 9, bool_Race_Uses_Troops, TRUE)
              2:
                call Add_Tech_Area("Fighters", 7, bool_Race_Uses_Fighters, TRUE)
              3:
                call Add_Tech_Area("Satellites", 5, bool_Race_Uses_Satellites, TRUE)
              4:
                call Add_Tech_Area("Weapon Platforms", 5, bool_Race_Uses_Weapon_Platforms, TRUE)
              5:
                call Add_Tech_Area("Mines", 5, bool_Race_Uses_Mines, TRUE)
              6:
                call Add_Tech_Area("Drones", 5, bool_Race_Uses_Drones, TRUE)
            endcase
          4:
            // Ship Support
            case Sys_Get_Random_Long(1, 8)
              1:
                call Add_Tech_Area("Space Yards", 11, TRUE, TRUE)
              2:
                call Add_Tech_Area("Repair", 11, TRUE, TRUE)
              3:
                call Add_Tech_Area("Vehicle Systems", 6, TRUE, TRUE)
              4:
                call Add_Tech_Area("Computers", 2, TRUE, TRUE)
              5:
                call Add_Tech_Area("Computers", 6, bool_Race_Uses_Master_Computers, TRUE)
              6:
                call Add_Tech_Area("Resupply", 20, TRUE, TRUE)
              7:
                call Add_Tech_Area("Stellar Harnessing", 6, TRUE, TRUE)
              8:
                call Add_Tech_Area("Gravitational Manipulation", 4, TRUE, TRUE)
            endcase
          5:
            // Engines
            case Sys_Get_Random_Long(1, 4)
              1:
                call Add_Tech_Area("Ion Engines", 3, TRUE, TRUE)
              2:
                call Add_Tech_Area("Contra-Terrene Engines", 3, TRUE, TRUE)
              3:
                call Add_Tech_Area("Jacketed-Photon Engines", 3, TRUE, TRUE)
              4:
                call Add_Tech_Area("Quantum Engines", 5, TRUE, TRUE)
            endcase
          6:
            // Defense Systems
            case Sys_Get_Random_Long(1, 7)
              1:
                call Add_Tech_Area("Armor", 20, bool_Race_Uses_Normal_Armor, TRUE)
              2:
                call Add_Tech_Area("Shields", 20, bool_Race_Uses_Normal_Shields, TRUE)
              3:
                call Add_Tech_Area("Planetary Defenses", 11, TRUE, TRUE)
              4:
                call Add_Tech_Area("Sensors", 20, TRUE, TRUE)
              5:
                call Add_Tech_Area("Defense Systems", 11, TRUE, TRUE)
              6:
                call Add_Tech_Area("Scanners", 11, TRUE, TRUE)
              7:  
                call Add_Tech_Area("Cloaking", 10, bool_Race_Uses_Cloaking_Devices, TRUE)
            endcase
          8:
            // Miscellaneous
            case Sys_Get_Random_Long(1, 8)
              1:
                call Add_Tech_Area("Cargo", 11, TRUE, TRUE)
              2:
                call Add_Tech_Area("Storage", 11, TRUE, TRUE)
              3:
                call Add_Tech_Area("Medical Treatment", 10, TRUE, TRUE)
              4:
                call Add_Tech_Area("Weapon Mounts", 1, TRUE, TRUE)
              5:
                call Add_Tech_Area("Ship Weapon Mounts", 11, TRUE, TRUE)
              6:
                call Add_Tech_Area("Base Weapon Mounts", 11, TRUE, TRUE)
              7:
                call Add_Tech_Area("Unit Weapon Mounts", 7, TRUE, TRUE)
              8:
                call Add_Tech_Area("Ship Capture", 11, TRUE, TRUE)
            endcase
          9:
            // Facility Improvement
            case Sys_Get_Random_Long(1, 11)
              1:
                call Add_Tech_Area("Applied Research", 11, TRUE, TRUE)
              2:
                call Add_Tech_Area("Advanced Military Science", 10, TRUE, TRUE)
              3:
                call Add_Tech_Area("Applied Political Science", 5, TRUE, TRUE)
              4:
                call Add_Tech_Area("Robotics", 9, TRUE, TRUE)
              5:
                call Add_Tech_Area("Centralized Computer Systems", 10, TRUE, TRUE)
              6:
                call Add_Tech_Area("Planetary Defenses", 11, TRUE, TRUE)
              7:
                call Add_Tech_Area("Planet Utilization", 15, TRUE, TRUE)
              8:
                call Add_Tech_Area("Resource Manipulation", 15, TRUE, TRUE)
              9:
                call Add_Tech_Area("Minerals Extraction", 15, TRUE, TRUE)
              10:
                call Add_Tech_Area("Organics Extraction", 15, TRUE, TRUE)
              11:
                call Add_Tech_Area("Radioactives Extraction", 15, TRUE, TRUE)
            endcase
          10:
            // Intelligence
            if (bool_Intel_Is_Allowed) then
              case Sys_Get_Random_Long(1, 5)
                1:
                  call Add_Tech_Area("Intelligence Services", 11, TRUE, TRUE)
                2:
                  call Add_Tech_Area("Sabotage - Ships & Fleets", 10, TRUE, TRUE)
                3:
                  call Add_Tech_Area("Sabotage - Planets & Colonies", 10, TRUE, TRUE)
                4:
                  call Add_Tech_Area("Sabotage - Empire Wide", 10, TRUE, TRUE)
                5:
                  call Add_Tech_Area("Sabotage - Political", 10, TRUE, TRUE)
              endcase
            endif
          11:
            // Stellar Manipulation
            call Add_Tech_Area("Stellar Manipulation", 6, TRUE, TRUE)
        endcase
      6:
        // Weapons
        case Sys_Get_Random_Long(1, 15)
          1:
            call Add_Tech_Area("Energy Stream Weapons", 11, bool_Race_Uses_Energy_Stream_Weapons, TRUE)
          2:
            call Add_Tech_Area("Energy Pulse Weapons", 11, bool_Race_Uses_Energy_Pulse_Weapons, TRUE)
          3:
            call Add_Tech_Area("Missile Weapons", 11, bool_Race_Uses_Missile_Weapons, TRUE)
          4:
            call Add_Tech_Area("Plasma Missile Weapons", 11, bool_Race_Uses_Plasma_Missile_Weapons, TRUE)
          5:
            call Add_Tech_Area("Focused-Energy Weapons", 11, bool_Race_Uses_Projectile_Weapons, TRUE)
          6:
            call Add_Tech_Area("Projectile Weapons", 11, bool_Race_Uses_Projectile_Weapons, TRUE)
          7:
            call Add_Tech_Area("Torpedo Weapons", 20, bool_Race_Uses_Torpedo_Weapons, TRUE)
          8:
            call Add_Tech_Area("Phased-Energy Weapons", 11, bool_Race_Uses_Phased_Energy_Weapons, TRUE)
          9:
            call Add_Tech_Area("High-Energy Discharge Weapons", 15, bool_Race_Uses_High_Energy_Discharge_Weapons, TRUE)
          10:
            call Add_Tech_Area("Crystalline Weapons", 20, bool_Race_Uses_Crystalline_Weapons, TRUE)
          11:
            call Add_Tech_Area("Interdimensional Weapons", 15, bool_Race_Uses_Interdimensional_Weapons, TRUE)
          12:
            call Add_Tech_Area("Organic Weapons", 20, bool_Race_Uses_Organic_Weapons, TRUE)
          13:
            call Add_Tech_Area("Psychic Weapons", 15, bool_Race_Uses_Psychic_Weapons, TRUE)
          14:
            call Add_Tech_Area("Temporal Weapons", 20, bool_Race_Uses_Temporal_Weapons, TRUE)
          15:
            case Sys_Get_Random_Long(1, 14)
              1:
                call Add_Tech_Area("Smaller Weapons", 6, TRUE, TRUE)
              2:
                call Add_Tech_Area("Troop Weapons", 11, TRUE, TRUE)
              3:
                call Add_Tech_Area("Warheads", 16, TRUE, TRUE)
              4:
                call Add_Tech_Area("Point-Defense Weapons", 11, TRUE, TRUE)
              5:
                call Add_Tech_Area("Tractor\Repulser Weapons", 11, bool_Race_Uses_Tractor_Repulser_Weapons, TRUE)
              6:
                call Add_Tech_Area("Shield Damaging Weapons", 15, bool_Race_Uses_Shield_Damaging_Weapons, TRUE)
              7:
                call Add_Tech_Area("Engine Overloading Weapons", 11, bool_Race_Uses_Engine_Overloading_Weapons, TRUE)
              8:
                call Add_Tech_Area("Weapon Overloading Weapons", 11, bool_Race_Uses_Weapon_Overloading_Weapons, TRUE)
              9:
                call Add_Tech_Area("Gravitational Weapons", 6, bool_Race_Uses_Gravitational_Weapons, TRUE)
              10:
                call Add_Tech_Area("Bombardment Weapons", 11, bool_Race_Uses_Bombardment_Weapons, TRUE)
              11:
                call Add_Tech_Area("Biological Weapons", 11, bool_Race_Uses_Biological_Weapons, TRUE)
              12:
                call Add_Tech_Area("Warp Weapons", 11, bool_Race_Uses_Warp_Weapons, TRUE)
              13:
                call Add_Tech_Area("Computer Combat", 3, bool_Race_Uses_Master_Computers, TRUE)
            endcase
        endcase
    endcase
    exitwhen (loop_count >= 1000) or (research_pts_left < 1000)
  endloop

  return TRUE
end